using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using dalCollection = Ultimate.DAL.CollectionClasses;
using dalFactory = Ultimate.DAL.FactoryClasses;
using dal = Ultimate.DAL;
using llbl = SD.LLBLGen.Pro.ORMSupportClasses;
using CommunityServer.Components;

namespace CommunityServerWeb.RegistrationSystem.League.Controls
{
    public partial class ViewLeagues : System.Web.UI.UserControl
    {

        protected  GridView grdLeagues;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadnBindData();
            }

        }

        /// <summary>
        /// Reloads the data to either show or hide inactive leagues.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkShowInactive_CheckedChanged(object sender, EventArgs e)
        {
            LoadnBindData();
        }

        private void LoadnBindData()
        {
            dalCollection.LeagueCollection leagues = new Ultimate.DAL.CollectionClasses.LeagueCollection();
            llbl.PredicateExpression filter = new llbl.PredicateExpression();

            if (!chkShowInactive.Checked)
            {//show inactive leagues
                filter.Add(dalFactory.PredicateFactory.CompareValue(
                    dal.LeagueFieldIndex.Active,
                    llbl.ComparisonOperator.Equal,
                    true));
            }
            if (!CSContext.Current.User.IsAdministrator)
            {//filter on the league admin.
                filter.Add(dalFactory.PredicateFactory.CompareRange(
                    dal.LeagueFieldIndex.RoleName,
                    CommunityServer.Components.Roles.GetUserRoleNames(CSContext.Current.UserName)
                    ));
            }

            leagues.GetMulti(filter);

            grdLeagues.DataSource = leagues;
            grdLeagues.DataBind();
        }
    }
}